/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.datatree;

import net.bioteam.appweb.gwt.apps.client.StaticHolder;

import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Encoding;
import com.smartgwt.client.types.FormMethod;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.HiddenItem;
import com.smartgwt.client.widgets.form.fields.UploadItem;
import com.smartgwt.client.widgets.layout.LayoutSpacer;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeNode;

public class UploadWindow extends Window
{

	final DynamicForm form;
	final HiddenItem pathItem;
	TreeNode node;
	final HiddenItem nodeIDItem;
	public UploadWindow()
	{

		setWidth(360);
		setHeight(360);
		setTitle("Upload Files");
		setShowMinimizeButton(false);
		setIsModal(true);
		centerInPage();
		setAutoSize(true);
		setLayoutMargin(30);
		setMembersMargin(30);
		
		addCloseClickHandler(new CloseClickHandler(){

			public void onCloseClick(CloseClientEvent event)
			{
				UploadWindow.this.hide();

			}});
		//this.setL
		//this.setLayoutBottomMargin(30);
		
		 form= new DynamicForm();
		form.setHeight100();
		form.setWidth(300);
		form.setPadding(5);
		
		//form.setBorder("2px solid red");
		form.setEncoding(Encoding.MULTIPART);
		form.setTarget("__gwt_upload");
		form.setMethod(FormMethod.POST);
		form.setAction("apps/user/fileupload");
		form.setNumCols(1);
		//form.setColWidths(300);
		

		FormItem items[]=new FormItem[12]; 
		
		//form.setLayoutAlign(VerticalAlignment.BOTTOM);
		for (int i=0; i<10; i++)
		{
			UploadItem ui=new UploadItem("_file"+i);
			ui.setShowTitle(false);
			ui.setWidth(300);
			items[i]=ui;
			
		}
		pathItem=new HiddenItem("path");
		nodeIDItem=new HiddenItem("nodeID");
		items[10]=pathItem;
		items[11]=nodeIDItem;
		
		form.setItems(items);
		form.setLayoutAlign(Alignment.CENTER);
		
		addItem(form);
		
		LayoutSpacer spacer=new LayoutSpacer();
		spacer.setHeight(20);
		addItem(spacer);
	
		
		IButton button=new IButton("upload");
		button.setLayoutAlign(Alignment.CENTER);
		button.addClickHandler(new ClickHandler(){

			public void onClick(ClickEvent event)
			{
				setCallback();
				form.submitForm();
				UploadWindow.this.hide();
				
			}});
		
		addItem(button);
	}
	
	
	public void myShowUp(String node_id, String path)
	{
		pathItem.setValue(path);
		nodeIDItem.setValue(node_id);
		//clean all file upload values.
		if (form.isDrawn())
		{
			form.redraw();
		}
		show();
	}
	
	public static void formCallback(String msg)
	{
		JSONValue value = JSONParser.parse(msg);
		JSONObject obj = value.isObject();
		if (!obj.get("status").isBoolean().booleanValue())
		{
			SC.warn(obj.get("msg").isString().stringValue(), StaticHolder.dumpB);
		} 
		if (obj.get("token")!=null)
		{
			String nodeID = obj.get("token").isString().stringValue();
			
			Tree tree=StaticHolder.getDataTreeGrid().getTree();
			TreeNode treeNode=tree.findById(nodeID);
			tree.reloadChildren(treeNode);
			tree.openFolder(treeNode);
		}
	}

	public static native void setCallback() /*-{
	    $wnd._jasonUploadCallback =
	       @net.bioteam.appweb.gwt.apps.client.datatree.UploadWindow::formCallback(Ljava/lang/String;);
	 }-*/;
}
